
import Vue from 'vue'
import VueRouter from 'vue-router'
// 导入 路由实例
import store from "@/store";
Vue.use(VueRouter)
const routes = [
  {
    path: '/',
    component: () => import('@/views/home/home.vue'),
    name: '默认打开页',
    meta: {
      index: 1
    }
  },
  {
    path: '/category',
    component: () => import('@/views/category/category.vue'),
    name: '分类页',
    meta: {
      index: 1
    }
  },
  {
    path: '/shopcart',
    component: () => import('@/views/shopcart/shopcart.vue'),
    name: '购物车页',
    meta: {
      index: 1,
      auth: true
    }
  },
  {
    path: '/user',
    component: () => import('@/views/user/user.vue'),
    name: '用户界面',
    meta: {
      index: 1,
      auth: true
    }
  },
  {
    path: '/login',
    component: () => import('@/views/login/login.vue'),
    name: '登录界面',
    meta: {
      index: 2
    }
  },
  {
    path: '/product',
    component: () => import('@/views/product/product.vue'),
    name: '商品列表页',
    meta: {
      index: 2
    }
  },
  {
    path: '/profile',
    component: () => import('@/views/userprofil/userprofil.vue'),
    name: '用户管理界面',
    meta: {
      index: 3,
      auth: true
    }
  },
  {
    path: '/Details',
    component: () => import('@/views/Details/Details.vue'),
    name: '商品详情页',
    meta: {
      index: 3
    }
  },
  {
    path: '/register',
    component: () => import('@/views/register/register.vue'),
    name: '注册页面',
    meta: {
      index: 3
    }
  },
  {
    path: '/proxy',
    name: 'Proxy', //QQ登录回调页面
    component: () => import('@/views/login/proxy.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  routes
})
// 设置路由守卫
router.beforeEach((to, from, next) => {
  // 判断当前用户 已登录 再次访问 登录界面 直接跳转到 个人中心页面
  if (to.name === '登录界面' && store.getters.getIsLogin) {
    next('/user')
  }
  // console.log(from);
  if (to.meta.auth) { // 需要校验用户是否登录的页面
    // 判断 用户是否登录
    if (store.getters.getIsLogin) {
      next()
    } else {
      // 未登录 跳转到登录页面
      next({ path: '/login' })
    }
  } else {
    next()
  }
})

export default router
